package com.ihome.android.market2.adapter;

import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

import com.ihome.android.http.HttpKeys;
import com.ihome.android.market2.frag.RecommAppFragment;
import com.ihome.android.market2.info.CategoryInfo;
import com.ihome.android.market2.view.LoopViewPager;
import com.ihome.android.util.log.LogUtil;

/**
 * FragmentPager Adapter to show Fragments 
 * @author Emerson
 *
 */
public class MainFragmentAdapter extends FragmentStatePagerAdapter {

	private static final String	TAG	= MainFragmentAdapter.class.getSimpleName();
	
	public List<CategoryInfo>	mListFrag;
	
	public MainFragmentAdapter(FragmentManager fm) {
		super(fm);
	}

	public MainFragmentAdapter(FragmentManager fm, List<CategoryInfo> mListFrag) {
		super(fm);
		this.mListFrag = mListFrag;
	}


	@Override
	public Fragment getItem(int position) {
		
		LogUtil.w(TAG, " -->  getItem() position = " + position);
		position = LoopViewPager.toRealPosition(position, getCount());
		LogUtil.w(TAG, " -->  getItem() real position = " + position);
		
		CategoryInfo info = mListFrag.get(position);
        return newPage(info);
	}

	@Override
	public int getCount() {
		return mListFrag.size();
	}
	
	private RecommAppFragment newPage(CategoryInfo info){
		
		RecommAppFragment appViewFragment = new RecommAppFragment();
		Bundle b = new Bundle();
		b.putInt(HttpKeys.KEY_SECTIONID, info.getContentId());
		appViewFragment.setArguments(b);
		return appViewFragment;
	}
	
}
